<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <div>
        <el-form-item label="渠道名称">
          <el-input v-model="queryParams.tradeQudao" placeholder="请输入渠道名称"></el-input>
        </el-form-item>
        <el-form-item label="交易结果">
          <el-select v-model="queryParams.tradeResult" placeholder="请选择">
            <el-option v-for="item in tradeResultList" :label="item.dictLabel" :value="item.dictValue"></el-option>
          </el-select>
        </el-form-item>
      </div>

      <div>
        <el-form-item label="对账批次">
          <el-input v-model="queryParams.batchNo" placeholder="请输入对账批次"></el-input>
        </el-form-item>

        <el-form-item label="交易金额">
          <el-input v-model="queryParams.amount" placeholder="请输入交易金额"></el-input>
        </el-form-item>
      </div>

      <div>
        <el-form-item label="是否差错">
          <el-select v-model="queryParams.hasDiff" placeholder="请选择">
            <el-option v-for="item in hasDiffList" :label="item.dictLabel" :value="item.dictValue"></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="交易日期">
          <div>
            <el-date-picker
              v-model="queryParams.tradeDateZf"
              type="datetime"
              value-format="yyyy-MM-dd HH:mm:ss"
              placeholder="选择日期时间">
            </el-date-picker>
            <label>至</label>
            <el-date-picker
              v-model="queryParams.tradeDateZf1"
              type="datetime"
              value-format="yyyy-MM-dd HH:mm:ss"
              placeholder="选择日期时间">
            </el-date-picker>
          </div>
        </el-form-item>
      </div>

      <div>
        <el-form-item label="处理结果">
          <el-select v-model="queryParams.processState" placeholder="请选择">
            <el-option v-for="item in processStateList" :label="item.dictLabel" :value="item.dictValue"></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="支付方式">
          <el-select v-model="queryParams.payWay" placeholder="请选择">
            <el-option v-for="item in payWayList" :label="item.dictLabel" :value="item.dictValue"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="cyan" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        </el-form-item>
      </div>

    </el-form>

    <el-table v-loading="loading" :data="detailList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="对账批次" align="center" prop="batchNo" />
      <el-table-column label="交易流水号" align="center" prop="payOrderNo" />
      <el-table-column label="渠道订单号" align="center" prop="qudaoOrderNo" />
      <el-table-column label="渠道名称" align="center" prop="tradeQudao" />
      <el-table-column label="交易结果" align="center" prop="tradeResult">
        <template slot-scope="scope">
          <span v-for="item in tradeResultList" v-if="scope.row.tradeResult==item.dictValue">{{item.dictLabel}}</span>
        </template>
      </el-table-column>
      <el-table-column label="交易金额" align="center" prop="amount" />
      <el-table-column label="交易日期" align="center" prop="tradeDateZf" />
      <el-table-column label="手续费" align="center" prop="commissionFee" />
      <el-table-column label="支付方式" align="center" prop="payWay">
        <template slot-scope="scope">
          <span v-for="item in payWayList" v-if="scope.row.payWay==item.dictValue">{{item.dictLabel}}</span>
        </template>
      </el-table-column>
      <el-table-column label="交易账户" align="center" prop="tradeAccount" />
      <el-table-column label="交易账户类型" align="center" prop="tradeAccountType" />
      <el-table-column label="对账完成时间" align="center" prop="checkEndDate" width="180"/>
      <el-table-column label="是否差错" align="center" prop="hasDiff">
        <template slot-scope="scope">
          <span v-for="item in hasDiffList" v-if="scope.row.hasDiff==item.dictValue">{{item.dictLabel}}</span>
        </template>
      </el-table-column>
      <el-table-column label="差错处理结果" align="center" prop="processState">
        <template slot-scope="scope">
          <span v-for="item in processStateList" v-if="scope.row.processState==item.dictValue">{{item.dictLabel}}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <span v-if="scope.row.processState==1 && scope.row.hasDiff==true">
             <el-button
               type="info"
               @click="errorHanding(scope.row)"
             >差错处理</el-button>
          </span>
        </template>
      </el-table-column>
    </el-table>

    <el-button type="warning" @click="checkBillBatch">重新对账</el-button>
    <el-button type="warning" @click="moneyForSure">对账确认</el-button>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />


  </div>
</template>

<script>
import { listDetail, getDetail, delDetail, addDetail, updateDetail,dictData,processCheckDifResult,checkBillInfo } from "@/api/finance/detail";
import {moneyInfo} from "../../../api/finance/batch";

export default {
  name: "Detail",
  components: {
  },
  data() {
    return {
      batchForm:{
        batchNo:null,
      },
      //交易结果集合
      tradeResultList:[],
      //是否差错
      hasDiffList:[],
      //处理结果
      processStateList:[],
      //支付方式
      payWayList:[],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 对账批次明细（保存对账结果明细的）表格数据
      detailList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        billDay: null,
        checkDay: null,
        batchId: null,
        batchNo: null,
        payOrderNo: null,
        qudaoOrderNo: null,
        tradeQudao: null,
        certificateJz: null,
        certificateDz: null,
        tradeResult: null,
        amount: null,
        tradeDateZf: null,
        tradeDateZf1:null,
        commissionFee: null,
        payWay: null,
        tradeAccount: null,
        tradeAccountType: null,
        checkEndDate: null,
        hasDiff: null,
        processState: null,
        completeState: null,
        confirmDate: null,
        canClearingAmount: null,
        remarks: null,
        reserve1: null,
        reserve2: null,
        reserve3: null,
        reserve4: null,
        reserve5: null,
        delFlg: null,
        createUserid: null,
        updateUserid: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        billDay: [
          { required: true, message: "账期年月日不能为空", trigger: "blur" }
        ],
        checkDay: [
          { required: true, message: "对账年月日(yyyymmdd)不能为空", trigger: "blur" }
        ],
        batchId: [
          { required: true, message: "批次ID不能为空", trigger: "blur" }
        ],
        batchNo: [
          { required: true, message: "批次号不能为空", trigger: "blur" }
        ],
        amount: [
          { required: true, message: "交易金额：支付订单金额不能为空", trigger: "blur" }
        ],
        payWay: [
          { required: true, message: "支付方式,同流水里面一样 1:微信扫码，2:支付宝扫码，3:微信公众号，4:认证支付，5:代付到银行卡不能为空", trigger: "blur" }
        ],
        createUserid: [
          { required: true, message: "创建用户ID不能为空", trigger: "blur" }
        ],
        createTime: [
          { required: true, message: "创建时间不能为空", trigger: "blur" }
        ],
        updateUserid: [
          { required: true, message: "更新用户ID不能为空", trigger: "blur" }
        ],
        updateTime: [
          { required: true, message: "更新时间不能为空", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.getList();
    this.getDictData();
  },
  methods: {
    //对账确认
    moneyForSure() {
      moneyInfo(this.batchForm).then(res=>{
        console.log("对账确认----",res)
        this.getList();
      })
    },
    //重新对账
    checkBillBatch() {
      checkBillInfo(this.batchForm).then(res=>{
          console.log("=========重新对账",res)
          this.getList();
      })
    },
    /** 查询对账批次明细（保存对账结果明细的）列表 */
    getList() {
      this.loading = true;
      listDetail(this.queryParams).then(response => {
        this.detailList = response.rows;
        this.batchForm.batchNo=this.detailList[0].batchNo
        console.log("===========================",this.batchForm.batchNo)
        this.total = response.total;
        this.loading = false;
      });
    },
    getDictData() {
      this.loading = true;
      dictData().then(res => {
        this.tradeResultList=res.data.tradeResultList.data
        this.hasDiffList=res.data.hasDiffList.data
        this.processStateList=res.data.processStateList.data
        this.payWayList=res.data.payWayList.data
      });
    },
    ////点击差错处理按钮进行处理差错值
    errorHanding(row) {
      if (confirm("是否进行差错处理")) {
        let payWayNum=null;
        console.log(row)
        if (row.processState != 0) {
          // 有商户可结算金额，并且金额大于0的时候
            if (row.canClearingAmount != '' && row.canClearingAmount > 0) {
                if (confirm("是否需要结算？")) {
                  payWayNum=2;
                  //差错处理方法
                  processCheckDifResult({batchId:row.id,processState:payWayNum}).then(res=>{
                    console.log("2=================>>>>",res)
                    if (res.code == 200) {
                     // this.$message.success("差错处理成功");
                      window.confirm("差错处理成功")
                      location.reload();
                    }else {
                      window.confirm(res.msg)
                     // this.$message.error(res.msg)
                    }
                  })
              }else {
                  payWayNum=3;
                  //差错处理方法
                  processCheckDifResult({batchId:row.id,processState:payWayNum}).then(res=>{
                    console.log("3=================>>>>",res)
                    if (res.code == 200) {
                      // this.$message.success("差错处理成功");
                      window.confirm("差错处理成功")
                      location.reload();
                    }else {
                      window.confirm(res.msg)
                      // this.$message.error(res.msg)
                    }
                  })
              }
            }else {// 无结算金额的时候，线下处理完差错，线上只修改一下处理状态，无需结算
                payWayNum=4;
                //处理差错
                processCheckDifResult({batchId:row.id,processState:payWayNum}).then(res=>{
                  console.log("4=================>>>>",res)
                  if (res.code == 200) {
                    // this.$message.success("差错处理成功");
                    window.confirm("差错处理成功")
                    location.reload();
                  }else {
                    window.confirm(res.msg)
                    // this.$message.error(res.msg)
                  }
                })

            }
        }
      }else {
        return;
      }

    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        billDay: null,
        checkDay: null,
        batchId: null,
        batchNo: null,
        payOrderNo: null,
        qudaoOrderNo: null,
        tradeQudao: null,
        certificateJz: null,
        certificateDz: null,
        tradeResult: null,
        amount: null,
        tradeDateZf: null,
        commissionFee: null,
        payWay: null,
        tradeAccount: null,
        tradeAccountType: null,
        checkEndDate: null,
        hasDiff: null,
        processState: null,
        completeState: null,
        confirmDate: null,
        canClearingAmount: null,
        remarks: null,
        reserve1: null,
        reserve2: null,
        reserve3: null,
        reserve4: null,
        reserve5: null,
        delFlg: null,
        createUserid: null,
        createTime: null,
        updateUserid: null,
        updateTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },


    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateDetail(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addDetail(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    }
  }
};
</script>
